package com.hyperfix.washer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyperfix.common.exception.CustomException;
import com.hyperfix.models.common.R;
import com.hyperfix.models.washer.dtos.StationDTO;
import com.hyperfix.models.washer.pojos.Pipeline;
import com.hyperfix.models.washer.pojos.Station;
import com.hyperfix.models.washer.vos.StationQueryVO;
import com.hyperfix.models.washer.vos.StationVO;
import com.hyperfix.washer.mapper.PipelineMapper;
import com.hyperfix.washer.mapper.StationMapper;
import com.hyperfix.washer.service.StationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author JiangChunYu
 * 2024/4/21
 */
@Transactional
@Service
public class StationServiceImpl extends ServiceImpl<StationMapper, Station> implements StationService {
    @Resource
    private StationMapper stationMapper;
    @Resource
    private PipelineMapper pipelineMapper;

    @Override
    public R<IPage<StationDTO>> list(Integer pageNum, Integer size, StationQueryVO stationQueryVO) {
        IPage<StationDTO> stationDTOIPage = new Page<>(pageNum, size);
        stationDTOIPage = stationMapper.listStation(stationDTOIPage, stationQueryVO);
        return R.success(stationDTOIPage);
    }

    @Override
    public R<String> updateStation(StationVO stationVO) {
        boolean isUpdated = stationMapper.updateStation(stationVO);
        if (!isUpdated) {
            throw  new CustomException("站场信息修改失败，站场不存在");
        }
        return R.success("站场信息修改成功");
    }

    @Override
    public R<List<Pipeline>> listStation(Integer stationId) {
        List<Pipeline> pipelines = pipelineMapper.selectList(new LambdaQueryWrapper<Pipeline>().eq(Pipeline::getStationId, stationId));
        return R.success(pipelines);
    }
}
